package com.softtown.modules.workflow.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.utils.Result;
import com.softtown.modules.workflow.dto.SwfPhaseNameDefDTO;
import com.softtown.modules.workflow.dto.SwfPhaseNameDefPageDTO;
import com.softtown.modules.workflow.entity.SwfDocPhaseDefEntity;
import com.softtown.modules.workflow.entity.SwfPhaseNameDefEntity;
import com.softtown.modules.workflow.entity.SwfProcPhaseEntity;
import com.softtown.modules.workflow.service.SwfDocPhaseDefService;
import com.softtown.modules.workflow.service.SwfPhaseNameDefService;
import com.softtown.modules.workflow.service.SwfProcPhaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/9/16 11:04
 * @Description:
 */
@RestController
@RequestMapping("swf/phaseNameDef")
@Api(tags = "获取该公司审批段名称定义")
public class SwfPhaseNameDefController {

    @Resource
    private SwfPhaseNameDefService swfPhaseNameDefService;

    @Resource
    private SwfDocPhaseDefService swfDocPhaseDefService;

    @Resource
    private SwfProcPhaseService swfProcPhaseService;

    /**
     * 审批段名称List
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.entity.SwfPhaseNameDefEntity>>
     * @Author Dingyayong
     * @Date 2020/4/13 17:14
     */
    @PostMapping("getAprvNameList")
    @ApiOperation("审批段名称List")
    @LogOperation("审批段名称List")
    public Result<List<SwfPhaseNameDefEntity>> getAprvNameList(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        List<SwfPhaseNameDefEntity> swfPhaseNameDefEntities =
                swfPhaseNameDefService.getAprvNameList(swfPhaseNameDefDTO);
        return new Result<List<SwfPhaseNameDefEntity>>().ok(swfPhaseNameDefEntities);
    }

    /**
     * 状态正常的审批段名称List
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result<java.util.List < com.softtown.modules.entity.SwfPhaseNameDefEntity>>
     * @Author Dingyayong
     * @Date 2020/4/13 17:14
     */
    @PostMapping("getAprvNameByStatusList")
    @ApiOperation("状态正常的审批段名称List")
    @LogOperation("状态正常的审批段名称List")
    public Result<List<SwfPhaseNameDefEntity>> getAprvNameByStatusList(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        List<SwfPhaseNameDefEntity> swfPhaseNameDefEntities =
                swfPhaseNameDefService.getAprvNameByStatusList(swfPhaseNameDefDTO);
        return new Result<List<SwfPhaseNameDefEntity>>().ok(swfPhaseNameDefEntities);
    }

    /**
     * 审批段名称定义分页
     *
     * @param swfPhaseNameDefPageDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:15
     */
    @PostMapping("getPhaseNameDefList")
    @ApiOperation("审批段名称定义分页")
    @LogOperation("审批段名称定义分页")
    public Result getPhaseNameDefList(@RequestBody SwfPhaseNameDefPageDTO swfPhaseNameDefPageDTO) {
        PageInfo<SwfPhaseNameDefEntity> page = swfPhaseNameDefService.getPhaseNameDefList(swfPhaseNameDefPageDTO);
        return new Result<PageInfo<SwfPhaseNameDefEntity>>().ok(page);
    }

    /**
     * 添加审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:16
     */
    @PostMapping("addPhaseNameDef")
    @ApiOperation("添加审批段名称定义")
    @LogOperation("添加审批段名称定义")
    public Result addPhaseNameDef(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        List<SwfPhaseNameDefEntity> swfPhaseNameDefEntity =
                swfPhaseNameDefService.getSwfPhaseNameByName(swfPhaseNameDefDTO.getName(),
                        swfPhaseNameDefDTO.getCompId());
        if (swfPhaseNameDefEntity.size() > 0) {
            return new Result<>().error(ErrorCode.DOC_PHASE_NAME_ERROR);
        } else {
            swfPhaseNameDefService.addPhaseNameDef(swfPhaseNameDefDTO);
        }
        return new Result<>();
    }

    /**
     * 激活停用审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:16
     */
    @PostMapping("activePhaseNameDef")
    @ApiOperation("激活停用审批段名称定义")
    @LogOperation("激活停用审批段名称定义")
    public Result activePhaseNameDef(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        swfPhaseNameDefService.activePhaseNameDef(swfPhaseNameDefDTO);
        return new Result<>();
    }

    /**
     * 编辑审批段名称定义
     *
     * @param swfPhaseNameDefDTO
     * @return
     */
    @PostMapping("updatePhaseNameDef")
    @ApiOperation("编辑审批段名称定义")
    @LogOperation("编辑审批段名称定义")
    public Result updatePhaseNameDef(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        List<SwfPhaseNameDefEntity> swfPhaseNameDefEntity =
                swfPhaseNameDefService.getSwfPhaseNameByName(swfPhaseNameDefDTO.getName(),
                        swfPhaseNameDefDTO.getCompId());
        if (swfPhaseNameDefEntity.size() > 0) {
            return new Result<>().error(ErrorCode.DOC_PHASE_NAME_ERROR);
        } else {
            swfPhaseNameDefService.updatePhaseNameDef(swfPhaseNameDefDTO);
        }
        return new Result<>();
    }

    /**
     * 删除审批段名称定义
     *
     * @param swfPhaseNameDefDTO 公司审批段定义DTO
     * @return com.softtown.common.utils.Result
     * @Author Dingyayong
     * @Date 2020/4/13 17:19
     */
    @PostMapping("deletePhaseNameDef")
    @ApiOperation("删除审批段名称定义")
    @LogOperation("删除审批段名称定义")
    public Result deletePhaseNameDef(@RequestBody SwfPhaseNameDefDTO swfPhaseNameDefDTO) {
        Result result = new Result<>();
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntityList =
                swfDocPhaseDefService.getDocPhaseDefByphaseId(swfPhaseNameDefDTO.getId());
        List<SwfProcPhaseEntity> swfProcPhaseEntities =
                swfProcPhaseService.getSwfProcPhaseByPhaseIdList(swfPhaseNameDefDTO.getId());
        if (swfDocPhaseDefEntityList.size() > 0 && swfProcPhaseEntities.size() > 0) {
            return result.error(ErrorCode.DOC_PHASE_ERROR);
        } else {
            swfPhaseNameDefService.deletePhaseNameDef(swfPhaseNameDefDTO);
        }
        return result;
    }
}
